import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * currency - 币种定义Modal
 * @date: 2018-7-3
 */
import { getResponse, createPagination } from 'utils/utils';
import { queryCurrency, addCurrency as _addCurrency, updateCurrency as _updateCurrency, fetchDetail as _fetchDetail } from "../services/currencyService";
export default {
  namespace: 'currency',
  state: {
    data: {
      list: [],
      pagination: {}
    },
    detail: {
      currencyCode: '',
      currencyName: '',
      list: []
    },
    currencyDetail: {},
    code: {}
  },
  effects: {
    fetchCurrencyList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchCurrencyList(_ref, _ref2) {
      var payload, call, put, response, data;
      return _regeneratorRuntime.wrap(function fetchCurrencyList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(queryCurrency, payload);

            case 4:
              response = _context.sent;
              data = getResponse(response);

              if (!data) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'queryCurrency',
                payload: data
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchCurrencyList);
    }),
    addCurrency: /*#__PURE__*/_regeneratorRuntime.mark(function addCurrency(_ref3, _ref4) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function addCurrency$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(_addCurrency, payload);

            case 4:
              response = _context2.sent;
              return _context2.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, addCurrency);
    }),
    updateCurrency: /*#__PURE__*/_regeneratorRuntime.mark(function updateCurrency(_ref5, _ref6) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function updateCurrency$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(_updateCurrency, payload);

            case 4:
              response = _context3.sent;
              return _context3.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, updateCurrency);
    }),
    fetchDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDetail(_ref7, _ref8) {
      var payload, call, put, response, data;
      return _regeneratorRuntime.wrap(function fetchDetail$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call, put = _ref8.put;
              _context4.next = 4;
              return call(_fetchDetail, payload);

            case 4:
              response = _context4.sent;
              data = getResponse(response);

              if (!data) {
                _context4.next = 9;
                break;
              }

              _context4.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  currencyDetail: data
                }
              });

            case 9:
              return _context4.abrupt("return", data);

            case 10:
            case "end":
              return _context4.stop();
          }
        }
      }, fetchDetail);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref9) {
      var payload = _ref9.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    queryCurrency: function queryCurrency(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        data: _objectSpread(_objectSpread({}, action.payload), {}, {
          list: action.payload.content,
          pagination: createPagination(action.payload)
        })
      });
    },
    currencyDetail: function currencyDetail(state, action) {
      return _objectSpread(_objectSpread({}, state), {}, {
        detail: _objectSpread(_objectSpread({}, action.payload), {}, {
          list: action.payload.content,
          currencyCode: action.payload.content[0] && action.payload.content[0].currencyCode,
          currencyName: action.payload.content[0] && action.payload.content[0].currencyName
        })
      });
    }
  }
};